one-of

 

One-of is used to randomly select only one agent out of an agentset or a list. It takes the form of one-of agentset/list, for example ask one-of patches [ set pcolor red ] will turn one random patch red. One-of can also randomly select one item from a provided list. For example, ask turtles [set color one-of [ red yellow blue ] ] will change the color of each turtle randomly to either red, yellow, or blue.

If one-of is used on an agentset which has no agents (for example, ask turtles with [color = red] but there are no red turtles), an error will occur. See nobody for how to prevent the error.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the one-of primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the one-of primitive:
 
 
Similar primitives:
n-of

randomly report "n" agents from an agentset

Read more
random

generate a random whole number within a specified range

Read more
max-n-of

Gives a list of top values of a list.

Read more
 
Learn another primitive